<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click='showDialog'>添加分类</el-button>
        </el-col>
      </el-row>

      <Tree class='tree' :data='tableData' :columns='treeCol' :selection-type='false' :expand-type='false' show-index index-text='' border >
        <template slot='deleted' slot-scope='data'>
          <i class="el-icon-success" v-if='data.row.cat_deleted == false' style='color:lightgreen; font-size:20px;'></i>
          <i class="el-icon-error" v-else style='color:lightred; font-size:20px;'></i>
        </template>
        <template slot='level' slot-scope='data'>
          <el-tag v-if='data.row.cat_level == 0'>一级</el-tag>
          <el-tag type='success' v-else-if='data.row.cat_level == 1'>二级</el-tag>
          <el-tag type='warning' v-else>三级</el-tag>
        </template>
        <template slot='opt'>
          <el-button type="primary" size='mini' icon="el-icon-edit">编辑</el-button>
              <el-button type="danger" size='mini' icon="el-icon-delete" >删除</el-button>
        </template>
      </Tree>
      
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%" @close='diaclose'>
      <el-form :model="addCatForm" :rules="addCatFormRules" ref="ref_addCatForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCatForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectList"
            :options="parentData"
            :props="cascader"
            @change="handleChange"
            clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCat">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return {
      dialogVisible:false,
      parentData:[],
      selectList:[],
      cascader:{ 
        checkStrictly:true,
        expandTrigger: 'hover',
        value:'cat_id',
        label:'cat_name',
        children:'children'
      },
      addCatForm:{
        cat_name:'',
        cat_pid:0,
        cat_level:0
      },
      addCatFormRules:{
        cat_name:[
          { required: true, message: '请输入分类的名称', trigger: 'blur' },
        ]
      },
      tableData:[],
      total:0,
      queryInfo:{
        type:3,
        pagenum:1,
        pagesize:5
      },
      treeCol:[
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'deleted',
        },
        {
          label: '排序',
          type: 'template',
          template: 'level',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ]
    }
  },
  methods: {
    async getData(){
      let res = await this.axios.get('/categories', {
        params:this.queryInfo
      })
      if( res.data.meta.status == 200 ) {
        this.tableData = res.data.data.result
        this.total = res.data.data.total
      } else {
        this.pop.error(res.data.meta.msg)
      }
    },
    handleSizeChange(id){
      this.queryInfo.pagesize = id
      this.getData()
    },
    handleCurrentChange(id){
      this.queryInfo.pagenum = id
      this.getData()
    },
    showDialog(){
      this.getParensData()
      this.dialogVisible = true
    },
    async getParensData(){
      let res = await this.axios.get('/categories', {
        params: {
          type:2
        }
      })
      if( res.data.meta.status == 200 ) {
        this.parentData = res.data.data
      } else {
        this.pop.error(res.data.meta.msg)
      }
    },
    handleChange(){
      if( this.selectList.length > 0 ) {
        this.addCatForm.cat_pid = this.selectList[this.selectList.length - 1]
        this.addCatForm.cat_level = this.selectList.length
      } else {
        this.addCatForm.cat_pid = 0
        this.addCatForm.cat_level = 0
      }
    },
    addCat(){
      this.$refs.ref_addCatForm.validate( async ip=>{
        if( !ip ) return
        let res = await this.axios.post('/categories', this.addCatForm)
        if( res.data.meta.status == 201 ) {
          this.pop.success( res.data.meta.msg )
          this.getData()
          this.dialogVisible = false
        } else {
          this.pop.error( res.data.meta.msg )
        }
      })
    },
    diaclose(){
      this.addCatForm.cat_name = ''
      this.addCatForm.cat_pid = 0
      this.addCatForm.cat_level = 0
      this.selectList = []
    }
  },
  created(){
    this.getData()
  }
}
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  padding:15px 0;
}
.tree {
  margin-top:20px;
}
</style>